
var netUtil = require('/../../utils/netUtil.js');
var util = require('/../../utils/util.js');
var app = getApp();

Page({
  
  data:{
  	isUpdate : false,
 		address : '',
 		phoneNum: '',
 		receiver : '',
 		provinceCityRegion : '',   //用户选择的省市区
 		oldAddress : null,
 		oldPhoneNum : null,
 		oldReceiver : null,
 		code : null
  },
  
  onLoad:function(options){
  	var that = this;
 		var code = options.code;
 		var oldAddress = options.address;
 		
 		var oldPhoneNum = options.phoneNum;
 		var oldReceiver = options.receiver;
 		if(code != undefined && code != null && code != '' && oldAddress != undefined && oldAddress != null && oldAddress != '') {
 			that.setData({
 				isUpdate : true,
 				oldAddress : oldAddress ,
 				oldPhoneNum : oldPhoneNum,
 				oldReceiver : oldReceiver,
 				code : code
 			});
 		}
 		
  },
  
  onReady:function(){
  
  },
  
  onShow:function(){
    
    // 页面显示
  },
  
  onHide:function(){
    // 页面隐藏
  },

  onUnload:function(){
    // 页面关闭

  },
  
  provinceCityRegionChange : function(e) {
  	var provinceCityRegion = e.detail.value;
  	var province = provinceCityRegion[0];
  	var city = provinceCityRegion[1];
  	var region = provinceCityRegion[2];
  	
  	this.setData({
  		provinceCityRegion : province + city + region
  	});
  },
  
  inputAddress : function(e) {
  	var address = e.detail.value;
  	this.setData({
  		address : address
  	});
  },
  
  inputReceiver : function(e) {
  	var receiver = e.detail.value;
  	this.setData({
  		receiver : receiver
  	});
  },
  
  inputPhoneNum : function(e) {
  	var phoneNum = e.detail.value;
  	this.setData({
  		phoneNum : phoneNum
  	});
  },
  
	backToAddressList : function() {
		wx.navigateTo({
				url: '/pages/address-list/address-list'
		});
	},
	
	addAddress : function() {
		var that = this;
		
		var isUpdate = that.data.isUpdate;
		
		var params = null;
	
		if(isUpdate) {
			params = {
  			method : 'wechat.wisen.address.userInfo.update',
  			openId : app.globalData.userInfo.openid,
  			data : {
  				address : that.data.provinceCityRegion + that.data.address,
  				phoneNum : that.data.phoneNum,
  				receiver : that.data.receiver,
  				code : that.data.code
  			}
  		};
		} else {
			params = {
  			method : 'wechat.wisen.address.userInfo.add',
  			openId : app.globalData.userInfo.openid,
  			data : {
  				address : that.data.provinceCityRegion + that.data.address,
  				phoneNum : that.data.phoneNum,
  				receiver : that.data.receiver
  			}
  		};
		}
  	
  	netUtil.buildRequest(that,'/wisen/api/wechat/ret',params,{
				
				onPre: function() {
					netUtil.showLoadingDialog(that);
				},
				
				onSuccess: function(resp) {
					
					if(resp.ret == app.globalData.respSuccessCode) {
			      	
			      	var tips = '';
			      	if(isUpdate) {
								tips = '修改成功';
							} else {
								tips = '添加成功';
							}
			      	
			      	netUtil.hideLoadingDialog(that);
			      	netUtil.showAlertDialog("提示", tips, false, "确定", null, function(){
			      			wx.navigateTo({
										url: '/pages/address-list/address-list'
									})
			      	});
			      	
 			    } else {
			      	if(resp.message != undefined && resp.message != null && resp.message != '') {
 			      		netUtil.showAlertDialog("提示", resp.message, false, "确定", null, null);
 			      	} else {
 			      		netUtil.showAlertDialog("提示", "网络请求失败", false, "确定", null, null);
 			      	}
			    }
				},
				
				onError: function(res) {
					console.log("fail");
					netUtil.hideLoadingDialog(that);
					netUtil.showAlertDialog("提示", msgCanShow, false, "确定", null, null);
				}
				
		}).send();
  	
	}


})